// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "URP/HealthPoint"
{
    Properties
    {
        _BaseMap ("Base Map", 2D) = "white" {}
        _BaseColor ("Base Color", Color) = (1,1,1,1)
        _BgColor ("Bg Color", Color) = (0,0,0,1) 
        _DialColor ("Dial Color", Color) = (0,0,0,1)
        _DialWidth ("Dial Width", Range(0, 0.5)) = 0.05
        _DialHeight ("Dial Height", Range(0, 1)) = 0.5
        _TotalHP ("Total HP", Float) = 300
        _PerUnit ("Per Unit", Float) = 100
        _Ratio ("Ratio", Range(0, 1)) = 0.5
    }

    SubShader
    {
        Tags
        {
            "RenderPipeline"="UniversalPipeline"
            "RenderType"="Transparent"
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        
        HLSLINCLUDE

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        TEXTURE2D (_BaseMap);       SAMPLER (sampler_BaseMap);

        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        half4 _BaseColor;
        half4 _BgColor;
        half4 _DialColor;
        half _DialWidth;
        half _DialHeight;
        half _TotalHP;
        half _PerUnit;
        half _Ratio;
        CBUFFER_END

        #ifdef UNITY_DOTS_INSTANCING_ENABLED
            UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)
                UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor)
                UNITY_DOTS_INSTANCED_PROP(float4, _BgColor)
                UNITY_DOTS_INSTANCED_PROP(float4, _DialColor)
                UNITY_DOTS_INSTANCED_PROP(float, _DialWidth)
                UNITY_DOTS_INSTANCED_PROP(float, _DialHeight)
                UNITY_DOTS_INSTANCED_PROP(float, _TotalHP)
                UNITY_DOTS_INSTANCED_PROP(float, _PerUnit)
                UNITY_DOTS_INSTANCED_PROP(float, _Ratio)
            UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)

            #define _BaseColor          UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_BaseColor)
            #define _BgColor            UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_BgColor)
            #define _DialColor          UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_DialColor)
            #define _DialWidth          UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_DialWidth)
            #define _DialHeight         UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_DialHeight)
            #define _TotalHP            UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_TotalHP)
            #define _PerUnit            UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_PerUnit)
            #define _Ratio              UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Ratio)
        #endif
        
        struct a2v {
            float4 positionOS : POSITION;
            float3 texcoord : TEXCOORD;
            UNITY_VERTEX_INPUT_INSTANCE_ID 
        };

        struct v2f {
            float4 positionCS : SV_POSITION;
            float2 texcoord : TEXCOORD0;
            float4 positionWS : TEXCOORD1;
            UNITY_VERTEX_INPUT_INSTANCE_ID
        };
        ENDHLSL

        Pass
        {
            Name "Default"
            Tags{ "LightMode"="UniversalForward" }
            
            ZWrite ON
            Blend SrcAlpha OneMinusSrcAlpha
            
            HLSLPROGRAM

            #pragma target 4.5
            #pragma multi_compile _ DOTS_INSTANCING_ON
            
            #pragma vertex vert
            #pragma fragment frag
            
            v2f vert(a2v i) {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(i);
                UNITY_TRANSFER_INSTANCE_ID(i, o);
                o.positionWS.xyz = TransformObjectToWorld(i.positionOS.xyz);
                o.positionCS = TransformWorldToHClip(o.positionWS.xyz);
                o.texcoord = TRANSFORM_TEX(i.texcoord.xy, _BaseMap);
                return o;
            }

            half4 frag(v2f i) : SV_TARGET {
                UNITY_SETUP_INSTANCE_ID(i);
                
                float2 uv = i.texcoord.xy;
                half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv) * _BaseColor;

                half width = _PerUnit * min(_DialWidth + 0.02 * _TotalHP/_PerUnit, 0.5);
                half first = 1 - step(width, uv.x * _TotalHP);
                half last = 1 - step(width, (1 - uv.x) * _TotalHP);
                half factor = frac(uv.x * _TotalHP/_PerUnit) * _PerUnit;
                 
                half dial = step(width, factor) + first + last + (1 - step(_DialHeight, uv.y));
                // dial = step(1, dial + step(0.95, uv.x) + step(uv.x, 0.05));
                
                dial = step(1, dial);
                color = lerp(_DialColor, color, dial);
                color = lerp(_BgColor, color, step(uv.x, _Ratio));
                
                return color;
            }
            ENDHLSL
        }
    }
}
